package dstation.server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import dstation.client.AddRobot;
import dstation.databeans.ExceptionBean;
import dstation.databeans.ResultBean;
import dstation.databeans.ResultDisplayBean;
import dstation.databeans.RobotBean;
import dstation.databeans.SensorBean;
import dstation.databeans.TestBean;
import dstation.model.ResultDAO;
import dstation.model.RobotDAO;
import dstation.model.SensorDAO;
import dstation.model.TestDAO;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AddRobotImpl extends RemoteServiceServlet implements AddRobot {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2505761732270766135L;

	private RobotDAO robotDAO;
	private SensorDAO sensorDAO;
	private ResultDAO resultDAO;
	private TestDAO testDAO;
	

	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	@Override
	public void init() throws ServletException {
		robotDAO = new RobotDAO("robot");
		sensorDAO = new SensorDAO("sensor");
		testDAO = new TestDAO("test");
		resultDAO = new ResultDAO("result", "robot", "test", "sensor");
		
	}
	
	@Override
	public RobotBean addRobot(RobotBean r) throws ExceptionBean{
		try {
			return robotDAO.insert(r);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public List<RobotBean> retrieveAll() throws ExceptionBean {
		try {
			return robotDAO.retrieveAll();
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public RobotBean lookupRobot(int id) throws ExceptionBean {
		try {
			return robotDAO.lookup(id);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public List<SensorBean> lookupSensors(int status) throws ExceptionBean {
		try {
			return sensorDAO.retrieve(status);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public List<ResultDisplayBean> lookupRobotResults(int id, int numResults, 
			String robotName, String testName, String sensorName) throws ExceptionBean {
		try {
			return resultDAO.getRobotTests(id,numResults,robotName,testName,sensorName);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("Exception: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public TestBean addTest(TestBean t) throws ExceptionBean {
		try {
			return testDAO.insert(t);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		} catch (IOException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("IOException: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public List<TestBean> getTestTable() throws ExceptionBean {
		try {
			return testDAO.getTestTable();
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public TestBean lookupTest(int id) throws ExceptionBean {
		try {
			return testDAO.lookup(id);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		} catch (IOException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("IOException: " + e.getMessage());
			throw b;
		} catch (ClassNotFoundException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("ClassNotFoundException: " + e.getMessage());
			throw b;
		} catch (Exception e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("Exception: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public TestBean updateTest(TestBean t) throws ExceptionBean {
		try {
			testDAO.update(t);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		} catch (IOException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("IOException: " + e.getMessage());
			throw b;
		}
		return null;
	}

	@Override
	public ResultBean runTest(ResultBean r, TestBean t) throws ExceptionBean {
		try {
			return resultDAO.runTest(r,t);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		} catch (IOException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("IOException: " + e.getMessage());
			throw b;
		} catch (ClassNotFoundException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("ClassNotFoundException: " + e.getMessage());
			throw b;
		}
	}

	@Override
	public Boolean testHasNoResults(int testid) throws ExceptionBean {
		try {
			return resultDAO.testHasNoResults(testid);
		} catch (SQLException e) {
			ExceptionBean b = new ExceptionBean();
			b.setMessage("SQLException: " + e.getMessage());
			throw b;
		}
	}
}
